<template>
  <a-modal
    class="profile-task-dialog"
    v-model:open="dialogVisible"
    width="37.5vw"
    :footer="null"
    :title="$t('dialog.task_to_order.title')"
  >
    <div class="task-box">
      <div class="task-allocate" v-if="!isAllocated">
        <div class="title">
          <span class="value"
            >{{ orderDetail.create_time.split(' ')[0] }} {{ orderDetail.title }}</span
          >
        </div>
        <div class="task-id">
          <span class="label">{{ $t("table.profile_task.col_id") }}:</span
          ><span class="value">{{ orderDetail.task_no }}</span>
        </div>
        <div class="user-select">
          <span class="label">{{
            $t("dialog.task_to_order.label_allocate_to")
          }}</span>
          <div class="control">
            <a-select v-model:value="selectValue">
              <a-select-option
                v-for="item in list"
                :key="item.id"
                :value="item.member_id"
                >{{
                  item.member_last_name +' '+ item.member_first_name
                }}</a-select-option
              >
            </a-select>
          </div>
        </div>
        <a-button
          type="primary"
          @click="allocateToUser"
          style="background: rgb(19, 194, 156)"
          >{{ $t("btnText.confirm_allocate") }}</a-button
        >
      </div>
      <!-- 分配成功 -->
      <div class="aollcate-success" v-else>
        <div
          v-html="
            $t('dialog.task_to_order.complete_descri').replace(
              '%',
              orderDetail.task_no
            )
          "
        ></div>
        <a-button
          type="primary"
          @click="dialogVisible = false"
          style="background: rgb(19, 194, 156)"
          >{{ $t("btnText.close") }}</a-button
        >
      </div>
    </div>
  </a-modal>
</template>

<script setup lang="ts">
import { computed, getCurrentInstance, nextTick, ref } from "vue";
import { postTaskOrderToUser } from "../../../api/task";
import { useMemberStore } from "../../../store";
import { message, Modal } from "ant-design-vue";

const $t = getCurrentInstance()?.appContext.config.globalProperties.$t;

type Props = {
  id: any;
  list: any;
  task_no: any;
  orderDetail?: any;
};

const isAllocated = ref(false);

const dialogVisible = ref(false);
const props = defineProps<Props>();
const showDialog = async () => {
  dialogVisible.value = true;
};

const emits = defineEmits(["allocate-success"]);

const selectValue = ref("");
const submiting = ref(false);
const allocateToUser = () => {
  if (selectValue.value == "")
    return message.warning($t("message.task_no_allocate_user"));
  let receiver = props.list.find((item) => item.member_id == selectValue.value);
  let receiver_name = receiver.member_last_name +' ' + receiver.member_first_name;
  let confirmModal = Modal.confirm({
    title: $t("dialog.task_to_order.title"),
    content: $t("dialog.task_to_order.modal_desc").replace("%", receiver_name),
    icon: () => h("", ""),
    centered: true,
    async onOk() {
      submiting.value = true;
      confirmModal.update({
        okButtonProps: {
          loading: submiting.value,
        },
      });
      let params = {
        task_id: props.id,
        member_id: selectValue.value,
      };
      let res = await postTaskOrderToUser(params);
      if (res.code === 200) {
        message.success($t("message.success.allocate"));
        isAllocated.value = true;
      } else {
        message.error($t("message.error.allocate"));
      }
      submiting.value = false;
      confirmModal.update({
        okButtonProps: {
          loading: submiting.value,
        },
      });
    },
    onCancel() {
    },
    class: "profile-confirm-modal",
    okText: $t("btnText.ensure"),
    cancelText: $t("btnText.cancel1"),
    width: "19.7917vw",
    maskClosable: true,
  });
};

watch(dialogVisible, (n) => {
  if (!n) {
    selectValue.value = "";
    if (isAllocated.value) {
      emits("allocate-success");
      setTimeout(() => {
        isAllocated.value = false;
      });
    }
  }
});

defineExpose({
  showDialog,
});
</script>
<style lang="less" scoped>
.task-box {
  font-size: 20px;
  min-height: 400px;

  .aollcate-success {
    font-size: 25px;
    text-align: center;
    height: 400px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 10px;
  }
}
.task-allocate {
  padding-top: 10%;

  & > div {
    display: flex;
    gap: 20px;
    align-items: center;
    & > *:last-child {
      flex: 1;
    }
    .control {
      display: flex;
      gap: 20px;
      :deep(.ant-select) {
        flex: 1;
        .ant-select-selector {
          height: 48px !important;
        }
        .ant-select-selection-item {
          line-height: 30px;
        }
      }
    }
  }
  .ant-btn {
    margin: 0 auto;
    margin-top: 20px;
  }
  span.label {
    text-align: right;
    width: 100px;
  }
  .title {
    padding-left: 40px;
    margin-bottom: 10px;
    font-size: 22px;
  }
  .task-id {
    margin-bottom: 20px;
  }
}
</style>
